package com.fc.test.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fc.test.mapper.auto.*;
import com.fc.test.model.auto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.fc.test.common.base.BaseService;
import com.fc.test.common.support.Convert;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.util.SnowflakeIdWorker;

/**
 * Service
* @Title: TAbbCommissionService.java 
* @Package com.fc.test.entity 
* @author fuce
* @email 87766867@qq.com
* @date 2019-08-15 17:49:51  
 */
@Service
public class TAbbCommissionService implements BaseService<TAbbCommission, TAbbCommissionExample>{
	@Autowired
	private TAbbCommissionMapper tAbbCommissionMapper;
	@Autowired
	private TAbbMemberMapper tAbbMemberMapper;
	@Autowired
	private TAbbOrderMapper tAbbOrderMapper;
	@Autowired
	private TAbbWithdrawMapper tAbbWithdrawMapper;
	@Autowired
	private TAbbCountMapper tAbbCountMapper;
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<Map> list(Tablepar tablepar,String name,int type,String starttime,String endtime){
	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<Map> list= tAbbCommissionMapper.selectByExample1(name,type,starttime,endtime);
	        PageInfo<Map> pageInfo = new PageInfo<Map>(list);
	        return  pageInfo;
	 }
	public PageInfo<Map> tongji(Tablepar tablepar, String searchTxt,String starttime,String endtime) {
		PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
		Map map=new HashMap<>();

		//算出某地区的总用户量
		TAbbMemberExample testExample=new TAbbMemberExample();
		TAbbMemberExample.Criteria criteria=testExample.createCriteria();
		if(searchTxt!=null&&!"".equals(searchTxt)){
			criteria.andAddressLike("%"+searchTxt+"%");
		}
		if(starttime!=null&&!"".equals(starttime)){
			criteria.andYuliu1GreaterThanOrEqualTo(starttime);
		}
		if(endtime!=null&&!"".equals(endtime)){
			criteria.andYuliu1LessThanOrEqualTo(endtime);
		}
		List<TAbbMember> list=tAbbMemberMapper.selectByExample(testExample);



		//算出未提现
		double allcount=tAbbMemberMapper.getAllCount();  //获取所有用户钱包里的总金额
		double daiTi=tAbbWithdrawMapper.getAllDaiTi();  //获取未提现的所有钱

		//算出某地区的总vip量
		TAbbMemberExample testExample1=new TAbbMemberExample();
		TAbbMemberExample.Criteria criteria1=testExample1.createCriteria();
		if(searchTxt!=null&&!"".equals(searchTxt)){
			criteria1.andAddressLike("%"+searchTxt+"%");
		}
		if(starttime!=null&&!"".equals(starttime)){
			criteria1.andYuliu2GreaterThanOrEqualTo(starttime);
		}
		if(endtime!=null&&!"".equals(endtime)){
			criteria1.andYuliu2LessThanOrEqualTo(endtime);
		}
		criteria1.andIsvipEqualTo(1);
		List<TAbbMember> list1=tAbbMemberMapper.selectByExample(testExample1);


		//算出某地区惠购商品的销售额
		String huigouPriceNumber;
		if (searchTxt==null || searchTxt.equals("")){
			huigouPriceNumber=tAbbOrderMapper.selectBytime(starttime,endtime,5);
		}else{
			huigouPriceNumber=tAbbOrderMapper.selectByAddressAndTime(searchTxt,starttime,endtime,5);
		}

		//算出某地区促销商品的销售额
		String cuixiaoPriceNumber;
		if (searchTxt==null || searchTxt.equals("")){
			cuixiaoPriceNumber=tAbbOrderMapper.selectBytime(starttime,endtime,3);
		}else{
			cuixiaoPriceNumber=tAbbOrderMapper.selectByAddressAndTime(searchTxt,starttime,endtime,3);
		}

		//算出某地区发布任务的销售额
		String renwuPriceNumber;
		if (searchTxt==null || searchTxt.equals("")){
			renwuPriceNumber=tAbbOrderMapper.selectBytime(starttime,endtime,-1);
		}else{
			renwuPriceNumber=tAbbOrderMapper.selectByAddressAndTime(searchTxt,starttime,endtime,-1);
		}

		//算出某地区发布任务的销售额
		String tixianPriceNumber;
		if (searchTxt==null || searchTxt.equals("")){
			tixianPriceNumber=tAbbWithdrawMapper.selectBytime(starttime,endtime);
		}else{
			tixianPriceNumber=tAbbWithdrawMapper.selectByAddressAndTime(searchTxt,starttime,endtime);
		}


		map.put("userNumber", list.size());
		map.put("vipNumber", list1.size());
		if (huigouPriceNumber==null){
			map.put("huigouPriceNumber", 0);
		}else{
			map.put("huigouPriceNumber", huigouPriceNumber);
		}
		if (cuixiaoPriceNumber==null){
			map.put("cuixiaoPriceNumber", 0);
		}else{
			map.put("cuixiaoPriceNumber", cuixiaoPriceNumber);
		}
		if (renwuPriceNumber==null){
			map.put("renwuPriceNumber", 0);
		}else{
			map.put("renwuPriceNumber", renwuPriceNumber);
		}
		if (tixianPriceNumber==null){
			map.put("tixianPriceNumber", 0);
		}else{
			map.put("tixianPriceNumber", tixianPriceNumber);
		}
		TAbbCount tAbbCount=tAbbCountMapper.selectLimitone();
		map.put("weitixianNumber",allcount+daiTi);
		map.put("zongshouyi",tAbbCount.getCount());//算出后台的总收益

		List<Map> datalist= new ArrayList<>();
		datalist.add(map);
		PageInfo<Map> pageInfo = new PageInfo<Map>(datalist);
		return  pageInfo;
	}
	@Override
	public int deleteByPrimaryKey(String ids) {
		List<String> lista=Convert.toListStrArray(ids);
		TAbbCommissionExample example=new TAbbCommissionExample();
		example.createCriteria().andIdIn(lista);
		return tAbbCommissionMapper.deleteByExample(example);
	}


	@Override
	public TAbbCommission selectByPrimaryKey(String id) {

		return tAbbCommissionMapper.selectByPrimaryKey(id);
	}


	@Override
	public int updateByPrimaryKeySelective(TAbbCommission record) {
		return tAbbCommissionMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 添加
	 */
	@Override
	public int insertSelective(TAbbCommission record) {
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
		return tAbbCommissionMapper.insertSelective(record);
	}










	@Override
	public int updateByExampleSelective(TAbbCommission record, TAbbCommissionExample example) {

		return tAbbCommissionMapper.updateByExampleSelective(record, example);
	}


	@Override
	public int updateByExample(TAbbCommission record, TAbbCommissionExample example) {

		return tAbbCommissionMapper.updateByExample(record, example);
	}

	@Override
	public List<TAbbCommission> selectByExample(TAbbCommissionExample example) {

		return tAbbCommissionMapper.selectByExample(example);
	}


	@Override
	public long countByExample(TAbbCommissionExample example) {

		return tAbbCommissionMapper.countByExample(example);
	}


	@Override
	public int deleteByExample(TAbbCommissionExample example) {

		return tAbbCommissionMapper.deleteByExample(example);
	}


}
